package xyz.lxbq.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import xyz.lxbq.pojo.Emp;
import xyz.lxbq.pojo.Student;
import xyz.lxbq.service.StudentServicre;
import xyz.lxbq.utils.PageUtils;
import xyz.lxbq.utils.ResultMessage;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author XuHu
 * @version 1.0
 * @date 2020/10/17 15:03
 */
@Controller
@Scope("prototype")
public class StudentController {

    @Autowired
    StudentServicre studentServicre;

    /**
     * 员工列表分页
     * @param pageIndex
     * @param pageSize
     * @param model
     * @return
     */
    @RequestMapping("/studentPageList/{pageIndex}/{pageSize}")
    String empPageList(@PathVariable("pageIndex") int pageIndex, @PathVariable("pageSize") int pageSize, @RequestParam(value="name",defaultValue = "") String name , @RequestParam(value = "classId", defaultValue = "0") Long classId, Model model){
        Map<String, Object> map = new HashMap<String, Object>();//将前端获取的参数，写入到这个HashMap中
        map.put("pageStart",(pageIndex-1)* pageSize);//pageStart表示该页显示的第一条数据是第几条，pageIndex表示当前是第几页
        map.put("pageSize",pageSize);
        map.put("name","%"+name+"%");
        map.put("classId",classId);

        int stydentcount = studentServicre.getStydentcount(map);//用hashmap中的name参数和classId参数来判断总共获取的数据条数
        List<Student> studentPage = studentServicre.getStudentPage(map);//用hashmap中的name参数和classId参数来获取所有的数据，
                                                                       //并根据pageStart和pageSize来确定当前页显示什么数据
        //封装工具类
        PageUtils pageUtils = new PageUtils(pageIndex,pageSize,stydentcount ,studentPage);//将4个参数传入工具类中，来确定总页数
        model.addAttribute("pageUtils",pageUtils);//将工具类中的七个属性放入model中，以用来返回给前端
        model.addAttribute("name",name);
        model.addAttribute("classId",classId);
        return "studentlist";
    }

    /**
     * 导出到excel
     */
    @RequestMapping("/exportexcel")
    String outexcel(@RequestParam(value = "name",defaultValue = "") String name,@RequestParam(value = "classId") Long classId){
        HashMap<String, Object> map = new HashMap<>();
        map.put("name","%"+name+"%");
        map.put("classId",classId);

        List<Student> students = studentServicre.getexcelPage(map);
        //需要将查询出来的数据，生成一个Excel表格，反馈给用户
        return "studentlist";
    }

    /**
     * 删除员工
     */
    @RequestMapping("/deletedStudent")
    @ResponseBody
    public ResultMessage deletedStudent(HttpServletResponse response, int id){
        ResultMessage message = null;
        response.setContentType("text/html;charset=utf-8");
        //存储新增部门
        int i = studentServicre.deletedStudent(id);
        if (i == 1 ){
            message = new ResultMessage(200,"删除成功");
        }else{
            message = new ResultMessage(300,"删除失败");
        }
        return message;
    }
    /**
     * 添加学生
     */
    @RequestMapping("/addStudent")
    @ResponseBody
    public ResultMessage addStudent(HttpSession session, HttpServletResponse response,String no, String name, long classId, String sex,  String email, String qq, String phone, String cardno, String school,String education,String birthday, String createdate ){

        ResultMessage message = null;
        response.setContentType("text/html;charset=utf-8");
        //存储新增学生
        Student addStu = new Student(no, name, classId, sex, email, phone, qq, cardno, school, education, birthday, createdate);
        int studentNo = studentServicre.getStudentNo(no);
        if(studentNo == 1){
            message = new ResultMessage(300, "账户已存在,请重新输入!");
        }else {
            int i = studentServicre.addStudent(addStu);
            System.out.println(i);
            //存session
            session.setAttribute("addStu",addStu);
            message = new ResultMessage(200, "添加成功。");
        }

        return message;
    }
}
